package cn.xunjie.ymcc.mq.consumer;

import cn.xunjie.ymcc.dto.PayNotifyDto;
import cn.xunjie.ymcc.service.ICourseUserLearnService;
import cn.xunjie.ymcc.util.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "course-paynotify-consumer",
        topic = "pay-notify-topic",
        selectorExpression = "pay-notify-tag",
        messageModel = MessageModel.BROADCASTING//广播模式

)
public class PayNotifyConumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseUserLearnService courseUserLearnService;
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        if(body == null || body.length == 0){
            return;
        }
        AssertUtil.isNotNull(body,"参数非法");
        String jsonStr = new String(body, StandardCharsets.UTF_8);
        PayNotifyDto payNotifyDto = JSONObject.parseObject(jsonStr, PayNotifyDto.class);
        if(StringUtils.isEmpty(payNotifyDto.getExtParams())){
            return;
        }
        courseUserLearnService.saveUserLear(payNotifyDto);


    }
}
